<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shadow &#38; Honnix &#187; Scala</title>
	<atom:link href="http://honnix.com/blog/archives/category/technology/scala/feed" rel="self" type="application/rss+xml" />
	<link>http://honnix.com/blog</link>
	<description>she&#039;s growing up</description>
	<lastBuildDate>Sun, 15 Jan 2012 10:44:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Scala与PowerMock</title>
		<link>http://honnix.com/blog/archives/753</link>
		<comments>http://honnix.com/blog/archives/753#comments</comments>
		<pubDate>Sat, 27 Aug 2011 12:30:38 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mockStatic]]></category>
		<category><![CDATA[PowerMock]]></category>
		<category><![CDATA[PrepareForTest]]></category>
		<category><![CDATA[RunWith]]></category>
		<category><![CDATA[ScalaTest]]></category>
		<category><![CDATA[Specs]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=753</guid>
		<description><![CDATA[这个题目对搜索引擎应该是比较友好的，虽然土了些。 PowerMock是个好东西，在某种程度上拯救了架构设计，使得我们不用过分考虑如何用复杂的架构设计来达到可以单元测试的目的。 Java里面如何使用PowerMock就不用多说了，随便google一下满屏幕都是，如果是同事还可以跟我要codekata的材料。但是这事儿换成Scala，情况就完全不同了。 首先要解决的问题是Scala用什么测试框架。比较流行的有ScalaTest和Specs，二者都支持BDD、ATDD，也就是这个BDD、ATDD把我折磨了一个晚上。 撇开ATDD不谈，我非常想认真尝试一下BDD，但是ScalaTest的BDD需要使用它自己的Runner，Specs的虽然支持JUnit，但是也需要用一个特殊的Runner，这对于PowerMock来说几乎是致命的。PowerMock＋Junit，必须使用PowerMockRunner才能mockStatic，mockNew，PowerMock＋TestNG倒是不需要Runner，但是ScalaTest不支持用TestNG来做BDD，而Specs根本就不支持TestNG。 有点儿混乱，总之一句话，现阶段，Scala＋BDD＋PowerMock这条路完全走不通。如果我的判断有问题，望指正。 那么就退一步，还是JUnit。从一个例子来说明我遭受到的痛苦以及整个解决过程。 1 2 3 4 5 6 7 8 9 10 11 12 public class MyStatic &#123; public static String hello&#40;&#41; &#123; // connect to twitter for example return tweet; &#125; &#125; &#160; public class HelloWorld &#123; public String helloWorld&#40;&#41; &#123; return MyStatic.hello&#40;&#41;; &#125; &#125; 上面一段代码，Java＋PowerMock很容易处理： 1 2 3&#8230;]]></description>
			<content:encoded><![CDATA[<p>这个题目对搜索引擎应该是比较友好的，虽然土了些。</p>
<p><a title="PowerMock" href="http://code.google.com/p/powermock/" target="_blank">PowerMock</a>是个好东西，在某种程度上<a title="单元测试拯救了架构设计？" href="http://honnix.com/blog/archives/747" target="_blank">拯救了架构设计</a>，使得我们不用过分考虑如何用复杂的架构设计来达到可以单元测试的目的。</p>
<p>Java里面如何使用PowerMock就不用多说了，随便google一下满屏幕都是，如果是同事还可以跟我要codekata的材料。但是这事儿换成Scala，情况就完全不同了。</p>
<p>首先要解决的问题是Scala用什么测试框架。比较流行的有<a title="ScalaTest" href="http://www.scalatest.org/" target="_blank">ScalaTest</a>和<a title="Specs" href="http://code.google.com/p/specs/" target="_blank">Specs</a>，二者都支持BDD、ATDD，也就是这个BDD、ATDD把我折磨了一个晚上。</p>
<p>撇开ATDD不谈，我非常想认真尝试一下BDD，但是ScalaTest的BDD需要使用它自己的Runner，Specs的虽然支持JUnit，但是也需要用一个特殊的Runner，这对于PowerMock来说几乎是致命的。PowerMock＋Junit，必须使用PowerMockRunner才能mockStatic，mockNew，PowerMock＋TestNG倒是不需要Runner，但是ScalaTest不支持用TestNG来做BDD，而Specs根本就不支持TestNG。</p>
<p>有点儿混乱，总之一句话，现阶段，Scala＋BDD＋PowerMock这条路完全走不通。如果我的判断有问题，望指正。</p>
<p>那么就退一步，还是JUnit。从一个例子来说明我遭受到的痛苦以及整个解决过程。</p>

<div class="wp_codebox"><table><tr id="p75315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p753code15"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyStatic <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// connect to twitter for example</span>
        <span style="color: #000000; font-weight: bold;">return</span> tweet<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> helloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> MyStatic.<span style="color: #006633;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>上面一段代码，Java＋PowerMock很容易处理：</p>

<div class="wp_codebox"><table><tr id="p75316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p753code16"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>PowerMockRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@PrepareForTest<span style="color: #009900;">&#40;</span>MyStatic.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> HelloWorldTest <span style="color: #009900;">&#123;</span>
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> testHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mockStatic<span style="color: #009900;">&#40;</span>MyStatic.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ....
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>如果是Scala，问题就接踵而至了。先看功能相似的Scala代码片段：</p>

<div class="wp_codebox"><table><tr id="p75317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p753code17"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> MyStatic <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> hello <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// connect to twitter for example</span>
    tweet
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> HelloWorld <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> helloWorld<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> MyStatic.<span style="color: #000000;">hello</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>@RunWith没有问题：</p>

<div class="wp_codebox"><table><tr id="p75318"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code18"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>RunWith<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PowerMockRunner<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>@PrepareForTest就开始出状况了。</p>
<p>首先貌似Scala对annotation的解释有些不太好，不能简单地传一个东西给@PrepareForTest，因为这玩意儿有两个参数，每个都是数组</p>

<div class="wp_codebox"><table><tr id="p75319"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p753code19"><pre class="java" style="font-family:monospace;">@Target<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> ElementType.<span style="color: #006633;">TYPE</span>, ElementType.<span style="color: #006633;">METHOD</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Documented
@Inherited
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> PrepareForTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> IndicateReloadClass.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> fullyQualifiedNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>所以不能：</p>

<div class="wp_codebox"><table><tr id="p75320"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code20"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>SomeClass<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>必须要：</p>

<div class="wp_codebox"><table><tr id="p75321"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code21"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>Array<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>SomeClass<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>这个还算是比较容易解决，但是对于Scala里的object，情况又有变化。object不支持classOf这个运算符，classOf[MyStatic]是非法的；MyStatic.getClass倒是可以拿到类，但这个类是另一个，名字是MyStatic$（后面会详细解释这两个类以及它们之间的关系）。但即使getClass得到的类是正确的，也没法直接传给annotation，因为annotation要求参数必须是一个constant，不能是一个方法调用，因此@PrepareForTest(Array(MyStatic.getClass))也是不行的。</p>
<p>怎么办呢？还好@PrepareForTest还有第二个参数，我们可以这样：</p>

<div class="wp_codebox"><table><tr id="p75322"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code22"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>fullyQualifiedNames <span style="color: #000080;">=</span> Array<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>虽然比较恶心，而且不利于重构，但是聊胜于无吧。</p>
<p>然后就是mockStatic的问题。怎么写呢？</p>

<div class="wp_codebox"><table><tr id="p75323"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code23"><pre class="scala" style="font-family:monospace;">mockStatic<span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>这肯定不行，因为之前说了getClass得到的类是MyStatic$。那么怎么样才能mock MyStatic呢？答案就是，反射。</p>

<div class="wp_codebox"><table><tr id="p75324"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code24"><pre class="scala" style="font-family:monospace;">mockStatic<span style="color: #F78811;">&#40;</span>Class.<span style="color: #000000;">forName</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>没有其他任何办法得到一个object的class信息，这里有一个很古老的<a title="annotation error" href="https://issues.scala-lang.org/browse/SI-2453" target="_blank">bug</a>。</p>
<p>好吧，两个问题都“解决”了，跑跑看。结果却是我想mock的东西根本就没有被mock。要命了。</p>
<p>事情还得从头说起。Scala对于一个object编译的结果是生成两个类，一个MyStatic，一个MyStatic$：</p>

<div class="wp_codebox"><table><tr id="p75325"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p753code25"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MyStatic
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> MyStatic$.<span style="color: #006633;">MODULE</span>$.<span style="color: #006633;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p75326"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p753code26"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MyStatic$
  <span style="color: #000000; font-weight: bold;">implements</span> ScalaObject
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span>  MODULE$<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MyStatic$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    MODULE$ <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这是反汇编的结果，稍微有点乱，凑合着看吧。不用多解释，代码已经很明白了。</p>
<p>问题是，我对MyStatic进行mock，理论上说也应该可以的，也就是mock之后就无视MyStatic$了。为什么不行呢？还得来看看调用地方的反汇编代码：</p>

<div class="wp_codebox"><table><tr id="p75327"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p753code27"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> testHelloWorld <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #006633;">MyStatic</span>$.<span style="color: #006633;">MODULE</span>$.<span style="color: #006633;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这里面压根儿就没有MyStatic的事儿了！MyStatic被华丽的忽视了！</p>
<p>到此为止终于真相大白了。咱们得这样来mock。</p>

<div class="wp_codebox"><table><tr id="p75328"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p753code28"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>RunWith<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PowerMockRunner<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>fullyQualifiedNames <span style="color: #000080;">=</span> Array<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic$&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> HelloWorldTest <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Test
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> testStartAndStop<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> mockMyStatic <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span><span style="color: #F78811;">&#41;</span>
    Whitebox.<span style="color: #000000;">setInternalState</span><span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span>, mockMyStatic<span style="color: #F78811;">&#41;</span>
    ...
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>想法很简单，把MyStatic$里面的MODULE$给换喽。</p>
<p>代码很丑陋，所以需要强调一下PowerMock应该被用在那些确实不需要复杂的依赖注入的地方来帮助简化架构设计，对于需要做依赖注入的地方，我们还是绝对不能松懈的，不能因为PowerMock啥都能mock就乱写代码。</p>
<p>顺便说一句，Specs对于Mockito已经有很好的支持了，加上Scala那种类似DSL的表达式，用起来很爽。希望PowerMock的作者能考虑支持Scala。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/753/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java调用Scala的一个真实案例</title>
		<link>http://honnix.com/blog/archives/750</link>
		<comments>http://honnix.com/blog/archives/750#comments</comments>
		<pubDate>Fri, 12 Aug 2011 15:26:23 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[invoke]]></category>
		<category><![CDATA[调用]]></category>
		<category><![CDATA[转换]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=750</guid>
		<description><![CDATA[Scala调用Java，完全没有问题；但是反过来总有那么一点不爽，或者必须要修改。这里是项目中一个真实的案例，拿出来分享一下。 1. 类型转换 1 2 3 4 5 object AsnLoader &#123; ... def batchLoadFile&#40;files: List&#91;String&#93;&#41;: Map&#91;String, AsnModule&#93; = files.foldLeft&#40;Map&#91;String, AsnModule&#93;&#40;&#41;&#41;&#40;&#40;x, y&#41; =&#62; x ++ load&#40;y&#41;&#41; ... &#125; 这里的List是Scala里的类，从Java里面调这个方法是不行的，所以需要弄一个wrapper方法： 1 2 3 import collection.JavaConversions._ ... def batchLoadFile&#40;files: java.util.List&#91;String&#93;&#41;: java.util.Map&#91;String, AsnModule&#93; = batchLoadFile&#40;asScalaBuffer&#40;files&#41;.toList&#41; 虽然asScalaBuffer本身就是个implicit，但是这里仍需要显示调用一下，否则编译器会认为batchLoadFile(file: java.util.List[String])更符合调用匹配规则，结果就变成了无限递归…… Java里面这样调用： 1 AsnLoader.batchLoadFile&#40;list&#41;; 这样看起来解决问题了，但是Eclipse的Scala插件会报一个莫名其妙的函数签名错误，其实根本就没错，直接maven或者用Intellij都是对的，Eclipse插件太烂或者太老。无奈只好把函数名也一起改了：batchLoadFileJ，这样就可以了。如果还不是很明白，去看看JavaConversions里面的implicit。 把Java的Map转换成Scala的immutale map稍微曲折了些，先转换成mutable map，再调用toMap方法转换成immutable map。 2. 构造方法 1 2&#8230;]]></description>
			<content:encoded><![CDATA[<p>Scala调用Java，完全没有问题；但是反过来总有那么一点不爽，或者必须要修改。这里是项目中一个真实的案例，拿出来分享一下。</p>
<p>1. 类型转换</p>

<div class="wp_codebox"><table><tr id="p75035"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p750code35"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> AsnLoader <span style="color: #F78811;">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> batchLoadFile<span style="color: #F78811;">&#40;</span>files<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> files.<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>x, y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> x ++ load<span style="color: #F78811;">&#40;</span>y<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>这里的List是Scala里的类，从Java里面调这个方法是不行的，所以需要弄一个wrapper方法：</p>

<div class="wp_codebox"><table><tr id="p75036"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p750code36"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">import</span></a> collection.<span style="color: #000000;">JavaConversions</span>.<span style="color: #000080;">_</span>
...
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> batchLoadFile<span style="color: #F78811;">&#40;</span>files<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">List</span><span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> batchLoadFile<span style="color: #F78811;">&#40;</span>asScalaBuffer<span style="color: #F78811;">&#40;</span>files<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toList</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>虽然asScalaBuffer本身就是个implicit，但是这里仍需要显示调用一下，否则编译器会认为batchLoadFile(file: java.util.List[String])更符合调用匹配规则，结果就变成了无限递归……</p>
<p>Java里面这样调用：</p>

<div class="wp_codebox"><table><tr id="p75037"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p750code37"><pre class="scala" style="font-family:monospace;">AsnLoader.<span style="color: #000000;">batchLoadFile</span><span style="color: #F78811;">&#40;</span>list<span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span></pre></td></tr></table></div>

<p>这样看起来解决问题了，但是Eclipse的Scala插件会报一个莫名其妙的函数签名错误，其实根本就没错，直接maven或者用Intellij都是对的，Eclipse插件太烂或者太老。无奈只好把函数名也一起改了：batchLoadFileJ，这样就可以了。如果还不是很明白，去看看JavaConversions里面的implicit。</p>
<p>把Java的Map转换成Scala的immutale map稍微曲折了些，先转换成mutable map，再调用toMap方法转换成immutable map。</p>
<p>2. 构造方法</p>

<div class="wp_codebox"><table><tr id="p75038"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p750code38"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> DefaultTransformer<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> asnModule<span style="color: #000080;">:</span> AsnModule, <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> dependencies<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map.<span style="color: #000000;">empty</span>,
                         <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> repository<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map.<span style="color: #000000;">empty</span>,
                         <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> messageHandler<span style="color: #000080;">:</span> MessageHandler <span style="color: #000080;">=</span> ConsoleMessageHandler<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">false</span></a><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>这里面的Map都是Scala的Map，所以从Java里不能直接调用，而Scala的构造方法又比较特别，重载的话只能减少参数个数而不能改变类型。这时候就需要companion object上场了。</p>

<div class="wp_codebox"><table><tr id="p75039"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p750code39"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> DefaultTransformer <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> apply<span style="color: #F78811;">&#40;</span>asnModule<span style="color: #000080;">:</span> AsnModule,
                 dependencies<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span>,
                 repository<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span>,
                 messageHandler<span style="color: #000080;">:</span> MessageHandler<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">new</span></a> DefaultTransformer<span style="color: #F78811;">&#40;</span>asnModule, dependencies, repository, messageHandler<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Java这样调用：</p>

<div class="wp_codebox"><table><tr id="p75040"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p750code40"><pre class="scala" style="font-family:monospace;">Transformer transformer <span style="color: #000080;">=</span> DefaultTransformer.<span style="color: #000000;">apply</span><span style="color: #F78811;">&#40;</span>...<span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span></pre></td></tr></table></div>

<p>3. Scala的bug</p>
<p>嗯，XML在什么语言里都是让人恶心的东西，即使Scala已经尽力做好了：<a href="https://issues.scala-lang.org/browse/SI-4865">https://issues.scala-lang.org/browse/SI-4865</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/750/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift的REST支持</title>
		<link>http://honnix.com/blog/archives/675</link>
		<comments>http://honnix.com/blog/archives/675#comments</comments>
		<pubDate>Mon, 14 Mar 2011 15:04:33 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JsonGet]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RestHandler]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=675</guid>
		<description><![CDATA[跟所有Lift的feature一样，支持REST也是非常非常得简单。 Mixin这个trait： 1 object AdminAPI extends RestHelper Boot.scala里面加上： 1 LiftRules.dispatch.append&#40;AdminAPI&#41; 如果不需要创建session（不创建著名的S对象），还可以： 1 LiftRules.statelessDispatchTable.append&#40;AdminAPI&#41; 然后就是URL匹配： 1 2 3 4 5 6 serve &#123; case &#34;api&#34; :: &#34;add&#34; :: Nil JsonGet _ =&#62; add case &#34;api&#34; :: &#34;delete&#34; :: Nil JsonGet _ =&#62; delete case &#34;api&#34; :: &#34;edit&#34; :: Nil JsonGet _ =&#62; edit case &#34;api&#34; ::&#8230;]]></description>
			<content:encoded><![CDATA[<p>跟所有Lift的feature一样，支持REST也是非常非常得简单。</p>
<p>Mixin这个trait：</p>

<div class="wp_codebox"><table><tr id="p67548"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p675code48"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> AdminAPI <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RestHelper</pre></td></tr></table></div>

<p>Boot.scala里面加上：</p>

<div class="wp_codebox"><table><tr id="p67549"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p675code49"><pre class="scala" style="font-family:monospace;">LiftRules.<span style="color: #000000;">dispatch</span>.<span style="color: #000000;">append</span><span style="color: #F78811;">&#40;</span>AdminAPI<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>如果不需要创建session（不创建著名的S对象），还可以：</p>

<div class="wp_codebox"><table><tr id="p67550"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p675code50"><pre class="scala" style="font-family:monospace;">LiftRules.<span style="color: #000000;">statelessDispatchTable</span>.<span style="color: #000000;">append</span><span style="color: #F78811;">&#40;</span>AdminAPI<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>然后就是URL匹配：</p>

<div class="wp_codebox"><table><tr id="p67551"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p675code51"><pre class="scala" style="font-family:monospace;">serve <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">case</span></a> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;add&quot;</span> <span style="color: #000080;">::</span> Nil JsonGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> add
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">case</span></a> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;delete&quot;</span> <span style="color: #000080;">::</span> Nil JsonGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> delete
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">case</span></a> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;edit&quot;</span> <span style="color: #000080;">::</span> Nil JsonGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> edit
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">case</span></a> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;get&quot;</span> <span style="color: #000080;">::</span> Nil JsonGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> get
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>以上都是GET的JSON接口。JsonGet会检查HTTP头里面的Accept以确认client是否支持JSON，这里需要注意的是“*/*”表示所有都接受（这个应该已经在2.3里面被修复掉了）；当然如果Accept里面不支持JSON，JsonGet还会去查询URL是否是.json结尾。</p>
<p>具体解释一下上面的例子。</p>
<p>对于URL为 http://server/api/add.json 的请求，第一条匹配规则命中，最后那个“_&#8221;是Req，然后调用add方法，add的返回值需要是LiftResponse类型；其实LiftReponse是个啥都不干的trait，有很多实现，JsonResponse、XmlResponse等等，总之几乎不用特别在意，Lift里面能作为response返回给client的都能用。</p>
<p>其他的匹配类似处理。</p>
<p>给个add方法的实现：</p>

<div class="wp_codebox"><table><tr id="p67552"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p675code52"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> add <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> linkId <span style="color: #000080;">=</span> param<span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">originUrl</span>.<span style="color: #000000;">name</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>x <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span>
    shortenedUrl.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">originUrl</span>.<span style="color: #000000;">name</span> -<span style="color: #000080;">&gt;</span> x<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">value</span><span style="color: #F78811;">&#41;</span> or <span style="color: #F78811;">&#123;</span>
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> tmp <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>DependencyFactory.<span style="color: #000000;">inject</span><span style="color: #F78811;">&#91;</span>NextIdGenerator<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">open_</span><span style="color: #000080;">!</span> <span style="color: #000080;">!?</span> <span style="color: #6666FF;">'id).toString
      shortenedUrl.createRecord.linkId(tmp).originUrl(x).shortUrl(Props.get(Site).open_! + &quot;/&quot; + tmp).date(new Date).
              ip(containerRequest.map(_.remoteAddress).toString).clickCount(0).save
      Full(tmp)
    }
  })
  (StatusField -&gt; linkId.map(_.map(x =&gt; SuccessStatus)).openOr(Full(FailedStatus)).openOr(FailedStatus)) ~
          (ShortenedUrl.linkId.name -&gt; linkId.openOr(Full(&quot;&quot;)).openOr(&quot;&quot;))
}</span></pre></td></tr></table></div>

<p>返回一个JSON对象{status: &#8220;successful&#8221;, linkId: &#8220;1&#8243;}。</p>
<p>Lift利用Scala对DSL的支持把JSON整得很舒服，几乎跟js原生的用起来没区别：</p>

<div class="wp_codebox"><table><tr id="p67553"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p675code53"><pre class="scala" style="font-family:monospace;"><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;name&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #6666FF;">&quot;honnix&quot;</span><span style="color: #F78811;">&#41;</span> ~
  <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;address&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #6666FF;">&quot;somewhere&quot;</span><span style="color: #F78811;">&#41;</span> ~
    <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;phone&quot;</span> -<span style="color: #000080;">&gt;</span> List<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;111&quot;</span>, <span style="color: #6666FF;">&quot;222&quot;</span>, <span style="color: #6666FF;">&quot;333&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>等价于：</p>

<div class="wp_codebox"><table><tr id="p67554"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p675code54"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;honnix&quot;</span><span style="color: #339933;">,</span>
 address<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;somewhere&quot;</span><span style="color: #339933;">,</span>
 phone<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;111&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;222&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;333&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>具爽吧？</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/675/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift的i18n</title>
		<link>http://honnix.com/blog/archives/669</link>
		<comments>http://honnix.com/blog/archives/669#comments</comments>
		<pubDate>Wed, 09 Mar 2011 12:37:26 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[国际化]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=669</guid>
		<description><![CDATA[基本上是转帖：http://www.assembla.com/wiki/show/liftweb/Internationalization。 但是还上要说一下，设计得太爽了。没怎么用过其他的web框架，不过Lift提供的真是好用。尤其是最后一点，template也可以直接国际化：index_en_US.html，index_zh_CN.html。不是所有都可以直接通过resource bundle来进行翻译，所以直接对template也就是页面本身来搞，真是很爽。]]></description>
			<content:encoded><![CDATA[<p>基本上是转帖：<a title="i18n" href="http://www.assembla.com/wiki/show/liftweb/Internationalization" target="_blank">http://www.assembla.com/wiki/show/liftweb/Internationalization</a>。</p>
<p>但是还上要说一下，设计得太爽了。没怎么用过其他的web框架，不过Lift提供的真是好用。尤其是最后一点，template也可以直接国际化：index_en_US.html，index_zh_CN.html。不是所有都可以直接通过resource bundle来进行翻译，所以直接对template也就是页面本身来搞，真是很爽。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/669/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift里进行MongoDB的MapReduce</title>
		<link>http://honnix.com/blog/archives/659</link>
		<comments>http://honnix.com/blog/archives/659#comments</comments>
		<pubDate>Tue, 08 Mar 2011 14:52:21 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[MapReduce]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=659</guid>
		<description><![CDATA[其实说Lift里不够准确，Lift对MapReduce没做什么封装，基本上就是直接调用mongo-java-driver的API。 举例说明，计算所有URL的点击次数： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def countClicks = &#123; MongoDB.useCollection&#40;shortenedUrl.collectionName&#41; &#123; x =&#38;gt; &#123; val map = &#34;&#34;&#34;function() { emit(&#34;totalClickCount&#34;, this.%s); }&#34;&#34;&#34; format ShortenedUrl.clickCount.name val reduce = &#34;&#34;&#34;function(key, values) { return Array.sum(values); }&#34;&#34;&#34; val results = x.mapReduce&#40;map, reduce, null, null&#41;.results &#160; /** * all&#8230;]]></description>
			<content:encoded><![CDATA[<p>其实说Lift里不够准确，Lift对MapReduce没做什么封装，基本上就是直接调用mongo-java-driver的API。</p>
<p>举例说明，计算所有URL的点击次数：</p>

<div class="wp_codebox"><table><tr id="p65956"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p659code56"><pre class="scala" style="font-family:monospace;">    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> countClicks <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      MongoDB.<span style="color: #000000;">useCollection</span><span style="color: #F78811;">&#40;</span>shortenedUrl.<span style="color: #000000;">collectionName</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        x <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> map <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;function() { emit(&quot;</span>totalClickCount<span style="color: #6666FF;">&quot;, this.%s); }&quot;</span><span style="color: #6666FF;">&quot;&quot;</span> format ShortenedUrl.<span style="color: #000000;">clickCount</span>.<span style="color: #000000;">name</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> reduce <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;function(key, values) { return Array.sum(values); }&quot;</span><span style="color: #6666FF;">&quot;&quot;</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> results <span style="color: #000080;">=</span> x.<span style="color: #000000;">mapReduce</span><span style="color: #F78811;">&#40;</span>map, reduce, <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">null</span></a>, <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">null</span></a><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">results</span>
&nbsp;
          <span style="color: #00ff00; font-style: italic;">/**
           * all numbers returned by mongodb is Double since this is how number defined by javascript
           */</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>results.<span style="color: #000000;">hasNext</span><span style="color: #F78811;">&#41;</span> results.<span style="color: #000000;">next</span>.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;value&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">asInstanceOf</span><span style="color: #F78811;">&#91;</span>Number<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">intValue</span>.<span style="color: #000000;">toString</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a> <span style="color: #6666FF;">&quot;0&quot;</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>map的时候把this.clickCount的值塞给&#8221;totalClickCount&#8221;这个key，reduce把所有这些值加起来，MongoDB会生成一个临时的collection，从里面选择“value”这个字段就可以了。</p>
<p>需要注意的是所有返回的数值类型都是Double，虽然BSON里对各种类型都有定义，但是目前为止MongoDB只支持返回Double。对js了解的人可能会比较清楚，但我就惨了。调了很长时间，一个bit一个bit的看，甚至还去MongoDB的JIRA上问：<a href="http://jira.mongodb.org/browse/SERVER-2688" target="_blank">http://jira.mongodb.org/browse/SERVER-2688</a>。土死了……</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/659/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift, MongoDB以及分页</title>
		<link>http://honnix.com/blog/archives/654</link>
		<comments>http://honnix.com/blog/archives/654#comments</comments>
		<pubDate>Tue, 08 Mar 2011 14:39:47 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[分页]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=654</guid>
		<description><![CDATA[说来惭愧，从来没写过分页的代码，知道是怎么回事儿，但没干过总归差了这么一点儿。 MongoDB和传统的数据库类似，提供了skip和limit，前者用来跳过一批记录，后者用来选择多少条。怎么用这里有很详细的解释：http://www.mongodb.org/display/DOCS/Advanced+Queries，就不多说了。重点讨论Lift里面怎么用。 分页通常有这么几个条件：排序字段、排序方式（升序还是降序）、每页显示条数、当前页面。这些参数可以通过URL传递，譬如：sort-by=linkId&#38;sort-order=-1&#38;perpage=3&#38;page=2，然后HTTP GET发送。 对于这种per request的参数，当然可以用的时候直接从著名的“S&#8221;里面取，也可以用RequestVar： 1 2 3 4 5 object page extends RequestVar&#91;Int&#93;&#40;S.param&#40;&#34;page&#34;&#41;.openOr&#40;&#34;1&#34;&#41;.toInt&#41; object perpage extends RequestVar&#91;Int&#93;&#40;S.param&#40;&#34;perpage&#34;&#41;.openOr&#40;&#34;10&#34;&#41;.toInt&#41; object offset extends RequestVar&#91;Int&#93;&#40;&#40;page - 1&#41; * perpage&#41; object sortBy extends RequestVar&#91;String&#93;&#40;S.param&#40;&#34;sort-by&#34;&#41;.openOr&#40;ShortenedUrl.linkId.name&#41;&#41; object sortOrder extends RequestVar&#91;Int&#93;&#40;S.param&#40;&#34;sort-order&#34;&#41;.openOr&#40;&#34;-1&#34;&#41;.toInt&#41; 这其中offset表示当前页的起始条目，-1表示降序，默认排序字段为linkId。 只有这些还不过瘾，我们再加上搜索条件： 关键字 点击次数 1 2 3 4 object search extends RequestVar&#91;String&#93;&#40;S.param&#40;&#34;search&#34;&#41;.openOr&#40;&#34;&#34;&#41;&#41; object searchIn extends RequestVar&#91;String&#93;&#40;S.param&#40;&#34;search-in&#34;&#41;.openOr&#40;ShortenedUrl.originUrl.name&#41;&#41; object clickFilter extends RequestVar&#91;String&#93;&#40;S.param&#40;&#34;click-filter&#34;&#41;.openOr&#40;&#34;gte&#34;&#41;&#41;&#8230;]]></description>
			<content:encoded><![CDATA[<p>说来惭愧，从来没写过分页的代码，知道是怎么回事儿，但没干过总归差了这么一点儿。</p>
<p>MongoDB和传统的数据库类似，提供了skip和limit，前者用来跳过一批记录，后者用来选择多少条。怎么用这里有很详细的解释：<a title="Advanced Query" href="http://www.mongodb.org/display/DOCS/Advanced+Queries" target="_blank">http://www.mongodb.org/display/DOCS/Advanced+Queries</a>，就不多说了。重点讨论Lift里面怎么用。</p>
<p>分页通常有这么几个条件：排序字段、排序方式（升序还是降序）、每页显示条数、当前页面。这些参数可以通过URL传递，譬如：sort-by=linkId&amp;sort-order=-1&amp;perpage=3&amp;page=2，然后HTTP GET发送。</p>
<p>对于这种per request的参数，当然可以用的时候直接从著名的“S&#8221;里面取，也可以用RequestVar：</p>

<div class="wp_codebox"><table><tr id="p65463"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p654code63"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> page <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;page&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;1&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toInt</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> perpage <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;perpage&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;10&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toInt</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> offset <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>page - <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">*</span> perpage<span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> sortBy <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;sort-by&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">name</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> sortOrder <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;sort-order&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;-1&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toInt</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>这其中offset表示当前页的起始条目，-1表示降序，默认排序字段为linkId。</p>
<p>只有这些还不过瘾，我们再加上搜索条件：</p>
<ol>
<li>关键字</li>
<li>点击次数</li>
</ol>

<div class="wp_codebox"><table><tr id="p65464"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p654code64"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> search <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;search&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> searchIn <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;search-in&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">originUrl</span>.<span style="color: #000000;">name</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> clickFilter <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;click-filter&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;gte&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> clickLimit <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>S.<span style="color: #000000;">param</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;click-limit&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openOr</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>默认搜索originUrl，点击次数大于等于某个值。</p>
<p>有过数据库搜索设计经验的人一般都会这么干：</p>

<div class="wp_codebox"><table><tr id="p65465"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p654code65"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">select</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">from</span> <span style="color: #993333; font-weight: bold;">table</span> <span style="color: #993333; font-weight: bold;">where</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>然后在后面拼搜索条件：</p>

<div class="wp_codebox"><table><tr id="p65466"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p654code66"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">and</span> originUrl <span style="color: #993333; font-weight: bold;">like</span> <span style="color: #ff0000;">'%google%'</span> <span style="color: #993333; font-weight: bold;">and</span> clickCount <span style="color: #66cc66;">&gt;=</span> <span style="color: #cc66cc;">10</span> sort <span style="color: #993333; font-weight: bold;">by</span> linkId skip <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">limit</span> <span style="color: #cc66cc;">10</span></pre></td></tr></table></div>

<p>我们如法炮制，不过稍微改变一下：</p>

<div class="wp_codebox"><table><tr id="p65467"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p654code67"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> clickObject <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>JObject<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>
    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>clickLimit.<span style="color: #000000;">isEmpty</span><span style="color: #F78811;">&#41;</span> JObject<span style="color: #F78811;">&#40;</span>Nil<span style="color: #F78811;">&#41;</span>
    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a> <span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">clickCount</span>.<span style="color: #000000;">name</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;$&quot;</span> + clickFilter<span style="color: #F78811;">&#41;</span> -<span style="color: #000080;">&gt;</span> clickLimit.<span style="color: #000000;">is</span>.<span style="color: #000000;">toInt</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#41;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> searchObject <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>JObject<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>search.<span style="color: #000000;">isEmpty</span><span style="color: #F78811;">&#41;</span> JObject<span style="color: #F78811;">&#40;</span>Nil<span style="color: #F78811;">&#41;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a> <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;$where&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;this.&quot;</span> + searchIn + <span style="color: #6666FF;">&quot;.indexOf('&quot;</span> + search + <span style="color: #6666FF;">&quot;') != -1&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#41;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> findOptions <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> RequestVar<span style="color: #F78811;">&#91;</span>List<span style="color: #F78811;">&#91;</span>FindOption<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>
  List<span style="color: #F78811;">&#40;</span>Skip<span style="color: #F78811;">&#40;</span>offset<span style="color: #F78811;">&#41;</span>, Limit<span style="color: #F78811;">&#40;</span>perpage<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#41;</span>
&nbsp;
shortenedUrl.<span style="color: #000000;">findAll</span><span style="color: #F78811;">&#40;</span>clickObject.<span style="color: #000000;">is</span> ~ searchObject.<span style="color: #000000;">is</span>,
    <span style="color: #F78811;">&#40;</span>sortBy.<span style="color: #000000;">is</span> -<span style="color: #000080;">&gt;</span> sortOrder.<span style="color: #000000;">is</span><span style="color: #F78811;">&#41;</span>, findOptions<span style="color: #000080;">:</span> <span style="color: #000080;">_*</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>如果没有搜索条件，返回Nil表示空的List；否则(x -&gt; y)构建一个JObject（这里是Scala的语法糖衣，背后发生了很多）；$where那里纯粹是MongoDB特有的，根本就是js；Skip和Limit跟SQL的含义完全相同。这样对最后一个statement的解读也就很自然了。</p>
<p>数据的事情搞定了，剩下就是页面处理，Lift再次表示了自己的牛逼：</p>

<div class="wp_codebox"><table><tr id="p65468"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p654code68"><pre class="scala" style="font-family:monospace;">    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> generateNav<span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> generateHref<span style="color: #F78811;">&#40;</span>page<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;/?search=&quot;</span> + search + <span style="color: #6666FF;">&quot;&amp;amp;sort-by=&quot;</span> + sortBy + <span style="color: #6666FF;">&quot;&amp;amp;sort-order=&quot;</span> + sortOrder +
              <span style="color: #6666FF;">&quot;&amp;amp;search-in=&quot;</span> + searchIn + <span style="color: #6666FF;">&quot;&amp;amp;click-filter=&quot;</span> + clickFilter + <span style="color: #6666FF;">&quot;&amp;amp;click-limit=&quot;</span> + clickLimit +
              <span style="color: #6666FF;">&quot;&amp;amp;perpage=&quot;</span> + perpage + <span style="color: #6666FF;">&quot;&amp;amp;page=&quot;</span> + page
&nbsp;
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> left <span style="color: #000080;">=</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>page.<span style="color: #000000;">is</span> <span style="color: #000080;">!=</span> <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>a title<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{&amp;quot;«&quot;</span> href<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{generateHref(page&quot;</span><span style="color: #000080;">&gt;</span>«<span style="color: #000080;">&lt;</span>/a<span style="color: #000080;">&gt;</span>
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a> Nil
&nbsp;
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> pages <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span> to totalPages<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toList</span>.<span style="color: #000000;">map</span> <span style="color: #F78811;">&#123;</span>
        x <span style="color: #000080;">=&gt;</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>x <span style="color: #000080;">==</span> page.<span style="color: #000000;">is</span><span style="color: #F78811;">&#41;</span>
            <span style="color: #000080;">&lt;</span>strong<span style="color: #000080;">&gt;</span>
              <span style="color: #F78811;">&#123;</span><span style="color: #6666FF;">&quot;[&quot;</span> + x + <span style="color: #6666FF;">&quot;]&quot;</span><span style="color: #F78811;">&#125;</span>
            <span style="color: #000080;">&lt;</span>/strong<span style="color: #000080;">&gt;</span>
          <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a>
            <span style="color: #000080;">&lt;</span>a title<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{&amp;quot;Page&quot;</span> href<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{generateHref(x)}&quot;</span><span style="color: #000080;">&gt;</span>
              <span style="color: #F78811;">&#123;</span>x<span style="color: #F78811;">&#125;</span>
            <span style="color: #000080;">&lt;</span>/a<span style="color: #000080;">&gt;</span>
      <span style="color: #F78811;">&#125;</span>
&nbsp;
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> right <span style="color: #000080;">=</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">if</span></a> <span style="color: #F78811;">&#40;</span>page.<span style="color: #000000;">is</span> <span style="color: #000080;">!=</span> totalPages<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>a title<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{&amp;quot;Go&quot;</span> href<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{generateHref(page.is&quot;</span><span style="color: #000080;">&gt;</span>»<span style="color: #000080;">&lt;</span>/a<span style="color: #000080;">&gt;</span>
      <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">else</span></a> Nil
&nbsp;
      left ++ pages ++ right
    <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>我写得比较烂，也就这么几行。效果就是这样：<a href="http://honnix.com/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-08-at-10.37.08-PM.png" rel="lightbox[654]"><img class="size-full wp-image-657" title="paging" src="http://honnix.com/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-08-at-10.37.08-PM.png" alt="" width="164" height="21" /></a>。</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/654/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift与MongoDB</title>
		<link>http://honnix.com/blog/archives/645</link>
		<comments>http://honnix.com/blog/archives/645#comments</comments>
		<pubDate>Tue, 01 Mar 2011 14:20:12 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=645</guid>
		<description><![CDATA[越来越多的公司和组织掺和到NoSQL运动里来，我也跟风玩玩儿MongoDB。 Lift对MongoDB已经有了很好的支持：http://www.assembla.com/wiki/show/liftweb/MongoDB 下面这段例子是一个非常简单的model设计： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class ShortenedUrl extends MongoRecord&#91;ShortenedUrl&#93; with MongoId&#91;ShortenedUrl&#93; &#123; def meta = ShortenedUrl &#160; object linkId extends StringField&#40;this, 10&#41; &#160; object originUrl extends StringField&#40;this, 500&#41; &#160; object shortUrl extends&#8230;]]></description>
			<content:encoded><![CDATA[<p>越来越多的公司和组织掺和到NoSQL运动里来，我也跟风玩玩儿MongoDB。</p>
<p>Lift对MongoDB已经有了很好的支持：http<a title="MongoDB" href="http://www.assembla.com/wiki/show/liftweb/MongoDB" target="_blank">://www.assembla.com/wiki/show/liftweb/MongoDB</a></p>
<p>下面这段例子是一个非常简单的model设计：</p>

<div class="wp_codebox"><table><tr id="p64572"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p645code72"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> ShortenedUrl <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> MongoRecord<span style="color: #F78811;">&#91;</span>ShortenedUrl<span style="color: #F78811;">&#93;</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">with</span></a> MongoId<span style="color: #F78811;">&#91;</span>ShortenedUrl<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> meta <span style="color: #000080;">=</span> ShortenedUrl
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> linkId <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> StringField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a>, <span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> originUrl <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> StringField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a>, <span style="color: #F78811;">500</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> shortUrl <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> StringField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a>, <span style="color: #F78811;">100</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> date <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> DateField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a><span style="color: #F78811;">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> ip <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> StringField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a>, <span style="color: #F78811;">15</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> clickCount <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> IntField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> ShortenedUrl <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> ShortenedUrl <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">with</span></a> MongoMetaRecord<span style="color: #F78811;">&#91;</span>ShortenedUrl<span style="color: #F78811;">&#93;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> NextId <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> MongoRecord<span style="color: #F78811;">&#91;</span>NextId<span style="color: #F78811;">&#93;</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">with</span></a> MongoId<span style="color: #F78811;">&#91;</span>NextId<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> meta <span style="color: #000080;">=</span> NextId
&nbsp;
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> next <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> StringField<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">this</span></a>, <span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> NextId <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">extends</span></a> NextId <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">with</span></a> MongoMetaRecord<span style="color: #F78811;">&#91;</span>NextId<span style="color: #F78811;">&#93;</span></pre></td></tr></table></div>

<p>这个东西是我仿照<a title="yourls" target="_blank" href="http://yourls.org/">yourls</a>做的数据模型。很简单，两个collection，一个记录缩短域名的所有信息，一个记录下一个URL的id是什么。</p>
<p>查找：</p>

<div class="wp_codebox"><table><tr id="p64573"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p645code73"><pre class="scala" style="font-family:monospace;">ShortenedUrl.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>ShortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">name</span> -<span style="color: #000080;">&gt;</span> id<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>存储：</p>

<div class="wp_codebox"><table><tr id="p64574"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p645code74"><pre class="scala" style="font-family:monospace;">currentShortenedUrl.<span style="color: #000000;">linkId</span><span style="color: #F78811;">&#40;</span>linkId<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">shortUrl</span><span style="color: #F78811;">&#40;</span>Props.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;site&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">open_</span><span style="color: #000080;">!</span> + <span style="color: #6666FF;">&quot;/&quot;</span> + linkId<span style="color: #F78811;">&#41;</span>.
                <span style="color: #000000;">ip</span><span style="color: #F78811;">&#40;</span>containerRequest.<span style="color: #000000;">open_</span><span style="color: #000080;">!</span>.<span style="color: #000000;">remoteAddress</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">clickCount</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">save</span></pre></td></tr></table></div>

<p>根本不用描述，太简单了。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/645/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>关于Lift的CSS Binding</title>
		<link>http://honnix.com/blog/archives/637</link>
		<comments>http://honnix.com/blog/archives/637#comments</comments>
		<pubDate>Tue, 01 Mar 2011 14:07:42 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CSS Binding]]></category>
		<category><![CDATA[TemplateFinder]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=637</guid>
		<description><![CDATA[具体怎么回事儿，这里写得很清楚，就不啰嗦了：http://www.assembla.com/wiki/show/liftweb/Binding_via_CSS_Selectors。 有一点需要特别说明一下：为了彻底把页面设计和后台代码分离，我希望所有和页面布局展示的地方都用静态html实现，然后在snippet里面作binding。这时候就需要TemplateFinder了。 看下面这段： 1 2 3 4 5 6 7 8 9 10 TemplateFinder.findAnyTemplate&#40;List&#40;TemplatesHidden, &#34;edit&#34;&#41;&#41; map &#123; &#34;#real-content ^^&#34;&#41;#&#62; &#34;true&#34; andThen &#34;#real-conent [id]&#34; #&#62; &#40;EditPrefix + shortenedUrl.linkId.value&#41; &#38; &#34;#edit-url [name]&#34; #&#62; urlId &#38; &#34;#edit-url [value]&#34; #&#62; shortenedUrl.originUrl.value &#38; &#34;#edit-url [id]&#34; #&#62;; urlId &#38; &#34;#save-button [onclick]&#34; #&#38;gt; js.toJsCmd &#38; &#34;#save-button [id]&#34; #&#62; &#40;EditPrefix + &#34;submit-&#34; +&#8230;]]></description>
			<content:encoded><![CDATA[<p>具体怎么回事儿，这里写得很清楚，就不啰嗦了：<a title="CSS Binding" href="http://www.assembla.com/wiki/show/liftweb/Binding_via_CSS_Selectors" target="_blank">http://www.assembla.com/wiki/show/liftweb/Binding_via_CSS_Selectors</a>。</p>
<p>有一点需要特别说明一下：为了彻底把页面设计和后台代码分离，我希望所有和页面布局展示的地方都用静态html实现，然后在snippet里面作binding。这时候就需要TemplateFinder了。</p>
<p>看下面这段：</p>

<div class="wp_codebox"><table><tr id="p63776"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p637code76"><pre class="scala" style="font-family:monospace;">TemplateFinder.<span style="color: #000000;">findAnyTemplate</span><span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#40;</span>TemplatesHidden, <span style="color: #6666FF;">&quot;edit&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> map <span style="color: #F78811;">&#123;</span>
  <span style="color: #6666FF;">&quot;#real-content ^^&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">#&gt;</span> <span style="color: #6666FF;">&quot;true&quot;</span> andThen
    <span style="color: #6666FF;">&quot;#real-conent [id]&quot;</span> <span style="color: #000080;">#&gt;</span> <span style="color: #F78811;">&#40;</span>EditPrefix + shortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">value</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#edit-url [name]&quot;</span> <span style="color: #000080;">#&gt;</span> urlId <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#edit-url [value]&quot;</span> <span style="color: #000080;">#&gt;</span> shortenedUrl.<span style="color: #000000;">originUrl</span>.<span style="color: #000000;">value</span> <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#edit-url [id]&quot;</span> <span style="color: #000080;">#&gt;;</span> urlId <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#save-button [onclick]&quot;</span> <span style="color: #000080;">#&amp;</span>gt<span style="color: #000080;">;</span> js.<span style="color: #000000;">toJsCmd</span> <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#save-button [id]&quot;</span> <span style="color: #000080;">#&gt;</span> <span style="color: #F78811;">&#40;</span>EditPrefix + <span style="color: #6666FF;">&quot;submit-&quot;</span> + shortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">value</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#cancel-button [onclick]&quot;</span> <span style="color: #000080;">#&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;hide_edit('&quot;</span> + shortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">value</span> + <span style="color: #6666FF;">&quot;')&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&amp;</span>
    <span style="color: #6666FF;">&quot;#cancel-button [id]&quot;</span> <span style="color: #000080;">#&gt;</span> <span style="color: #F78811;">&#40;</span>EditPrefix + <span style="color: #6666FF;">&quot;close-&quot;</span> + shortenedUrl.<span style="color: #000000;">linkId</span>.<span style="color: #000000;">value</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>edit.html是一个完整的静态html，用来显示一个table，上面代码做的事情就是选中id为real-content的这个节点，然后对它做CSS的binding以实现动态页面。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/637/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何输出完整的菜单系统</title>
		<link>http://honnix.com/blog/archives/628</link>
		<comments>http://honnix.com/blog/archives/628#comments</comments>
		<pubDate>Sat, 26 Feb 2011 13:32:19 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[expandAll]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[superfish]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=628</guid>
		<description><![CDATA[Lift默认不会输出整个菜单系统，只有top level，点击包含子项的菜单才会输出下面的层次。这样带来的问题是，如果使用superfish之类的js来控制和渲染就不行了。这个问题相当容易解决： 1 &#60;span class=&#34;lift:Menu.builder?expandAll&#34;&#62;&#60;/span&#62; 加上expandAll就行了。]]></description>
			<content:encoded><![CDATA[<p>Lift默认不会输出整个菜单系统，只有top level，点击包含子项的菜单才会输出下面的层次。这样带来的问题是，如果使用<a title="superfish" href="http://users.tpg.com.au/j_birch/plugins/superfish/" target="_blank">superfish</a>之类的js来控制和渲染就不行了。这个问题相当容易解决：</p>

<div class="wp_codebox"><table><tr id="p62878"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p628code78"><pre class="html" style="font-family:monospace;">&lt;span class=&quot;lift:Menu.builder?expandAll&quot;&gt;&lt;/span&gt;</pre></td></tr></table></div>

<p>加上expandAll就行了。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/628/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于Lift的一些事情</title>
		<link>http://honnix.com/blog/archives/626</link>
		<comments>http://honnix.com/blog/archives/626#comments</comments>
		<pubDate>Sat, 26 Feb 2011 13:22:56 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=626</guid>
		<description><![CDATA[最近在试着用Lift写一个缩URL的东西，由于这个东西相对来说比之前做的小玩意儿复杂一些，写几篇东西作为记录，省得总是要解决之前早就已经解决过的问题。]]></description>
			<content:encoded><![CDATA[<p>最近在试着用Lift写一个缩URL的东西，由于这个东西相对来说比之前做的小玩意儿复杂一些，写几篇东西作为记录，省得总是要解决之前早就已经解决过的问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/626/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

